package com.vastgames.mars.Entity;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.Circle;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.state.StateBasedGame;

/**
 * ControllableEntity ( The player or whatever)
 * Implements Controllable and Moveable (Must be able to move to control)
 * @author bgilb
 *
 */
abstract class ControllableEntity extends Entity implements Controllable, Moveable  {

	
	private Body body = null;
	private float speed=0;
	private int entityDirection = 0;
	
	public void updateControls(GameContainer gc) {
		if (gc.getInput().isKeyDown(Input.KEY_UP)
        		&& gc.getInput().isKeyDown(Input.KEY_LEFT))   {
        	move(Moveable.NORTHWEST, speed);
        }else
        if (gc.getInput().isKeyDown(Input.KEY_UP)
        		&& gc.getInput().isKeyDown(Input.KEY_RIGHT))   {
        	move(Moveable.NORTHEAST, speed);
        }else
        if (gc.getInput().isKeyDown(Input.KEY_DOWN)
        		&& gc.getInput().isKeyDown(Input.KEY_LEFT))   {
        	move(Moveable.SOUTHWEST, speed);
        }else
        if (gc.getInput().isKeyDown(Input.KEY_DOWN)
        		&& gc.getInput().isKeyDown(Input.KEY_RIGHT))   {
        	move(Moveable.SOUTHEAST, speed);
        } else
		if (gc.getInput().isKeyDown(Input.KEY_UP))   {
			move(Moveable.NORTH, speed);
        } else
        if (gc.getInput().isKeyDown(Input.KEY_DOWN)) {
        	move(Moveable.SOUTH, speed);
        }  else     
        if (gc.getInput().isKeyDown(Input.KEY_LEFT)) { 
        	move(Moveable.WEST, speed);
        }else
        if (gc.getInput().isKeyDown(Input.KEY_RIGHT)){
        	move(Moveable.EAST, speed);
        }
		
		
	}
	public void init(GameContainer gc, StateBasedGame sbg){
		
		//All Moveable entitys should be a circle
		body = new Body(new Circle(6f), 100.0f);
		//Rotation is useless in isometric game
		body.setRotatable(false);
		body.setMaxVelocity(200f, 200f);
		body.setDamping(32f);
	}
	public Body getBody() {
		return body;
	}
	/**
	 * Set the speed of the controllable entity
	 * @param s - Speed
	 */
	public void setSpeed(float s){
		speed=s;
	}
	public void move(int direction, float force){
		
		switch(direction){
		case NORTH: 
			body.addForce(new Vector2f(0,-force)); break;
		case SOUTH: 
			body.addForce(new Vector2f(0,force)); break;
		case WEST: 
			body.addForce(new Vector2f(-force,0)); break;
		case EAST: 
			body.addForce(new Vector2f(force,0)); break;
		case NORTHWEST: 
			body.addForce(new Vector2f(-force/1f,-force/2f)); break;
		case NORTHEAST: 
			body.addForce(new Vector2f(force/1f,-force/2f)); break;
		case SOUTHWEST: 
			body.addForce(new Vector2f(-force/1.5f,force/3)); break;
		case SOUTHEAST: 
			body.addForce(new Vector2f(force/1.5f,force/3)); break;
		}
		entityDirection=direction;
	}
}
